package com.zhq.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhq.utils.PR;
import com.zhq.entity.Cla;
import com.zhq.entity.Stu;
import com.zhq.mapper.StuMapper;
import com.zhq.service.IClassesService;
import com.zhq.service.IStudentService;
import com.zhq.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl extends ServiceImpl<StuMapper, Stu> implements IStudentService {
    @Autowired
    private IClassesService classesService;
    @Override
    public PR<Stu> findByPage(Integer page, Integer pageSize) {
        Page<Stu> page1 = this.page(new Page<>(page, pageSize));
        
        return new PR<>(page1.getRecords(),page1.getTotal());
    }
    
    @Override
    public PR<Stu> search(Integer page, Integer pageSize, StudentVo vo) {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid());
        Page<Stu> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            Cla classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        return new PR<>(page1.getRecords(),page1.getTotal());
    }
    @Override
    public boolean add(Stu student) {
        return this.save(student);
    }
}
